<template>
  <div class="direct-api-test-container">
    <h1>直接 API 测试</h1>
    
    <el-card class="test-card">
      <template #header>
        <div class="card-header">
          <span>直接 API 请求测试</span>
        </div>
      </template>
      
      <div class="test-actions">
        <el-button type="primary" @click="testDirectFetch" :loading="loading">
          直接使用 fetch 测试 API
        </el-button>
        
        <el-button type="primary" @click="testRootEndpoint" :loading="loading">
          测试根端点
        </el-button>
        
        <el-button type="primary" @click="testWithFullUrl" :loading="loading">
          使用完整 URL 测试
        </el-button>
      </div>
      
      <div v-if="result" class="result-container">
        <h3>API 响应结果：</h3>
        <pre>{{ typeof result === 'object' ? JSON.stringify(result, null, 2) : result }}</pre>
      </div>
      
      <div v-if="error" class="error-container">
        <h3>API 错误：</h3>
        <pre>{{ error }}</pre>
      </div>
    </el-card>
  </div>
</template>

<script setup>
import { ref } from 'vue'

const loading = ref(false)
const result = ref(null)
const error = ref(null)

const clearResults = () => {
  result.value = null
  error.value = null
}

const testDirectFetch = async () => {
  clearResults()
  loading.value = true
  
  try {
    const response = await fetch('/api/dashboard/summary')
    if (!response.ok) {
      throw new Error(`HTTP error! status: ${response.status}`)
    }
    const data = await response.json()
    result.value = data
    console.log('Direct fetch result:', data)
  } catch (err) {
    error.value = `Error: ${err.message}`
    console.error('Error with direct fetch:', err)
  } finally {
    loading.value = false
  }
}

const testRootEndpoint = async () => {
  clearResults()
  loading.value = true
  
  try {
    const response = await fetch('/')
    const text = await response.text()
    result.value = text
    console.log('Root endpoint result:', text)
  } catch (err) {
    error.value = `Error: ${err.message}`
    console.error('Error with root endpoint:', err)
  } finally {
    loading.value = false
  }
}

const testWithFullUrl = async () => {
  clearResults()
  loading.value = true
  
  try {
    const response = await fetch('http://localhost:5000/api/dashboard/summary')
    if (!response.ok) {
      throw new Error(`HTTP error! status: ${response.status}`)
    }
    const data = await response.json()
    result.value = data
    console.log('Full URL fetch result:', data)
  } catch (err) {
    error.value = `Error: ${err.message}`
    console.error('Error with full URL fetch:', err)
  } finally {
    loading.value = false
  }
}
</script>

<style scoped>
.direct-api-test-container {
  padding: 20px;
  background-color: #f5f7fa;
  height: 100%;
  overflow-y: auto;
}

h1 {
  margin-bottom: 20px;
}

.test-card {
  margin-bottom: 20px;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.test-actions {
  display: flex;
  gap: 10px;
  margin-bottom: 20px;
}

.result-container,
.error-container {
  margin-top: 20px;
  padding: 15px;
  border-radius: 4px;
}

.result-container {
  background-color: #f0f9eb;
  border: 1px solid #e1f3d8;
}

.error-container {
  background-color: #fef0f0;
  border: 1px solid #fde2e2;
  color: #f56c6c;
}

pre {
  white-space: pre-wrap;
  word-wrap: break-word;
  background-color: #f8f8f8;
  padding: 10px;
  border-radius: 4px;
  max-height: 400px;
  overflow-y: auto;
}
</style>
